<template>
  <el-main>
    <el-row type="flex">
      <el-col :span="3">
        <el-button type="primary" @click="handleFlash">刷新</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" @click="handleAdd">添加设备</el-button>
      </el-col>
    </el-row>

    <el-table :data="sliceDevices" style="width: 100%" max-height="600">
      <el-table-column label="设备名" width="200">
        <template slot-scope="scope">
          {{ scope.row.device }}
        </template>
      </el-table-column>
      <el-table-column label="坐标" width="200">
        <template slot-scope="scope">
          ({{ scope.row.x }}, {{ scope.row.y }})
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="230">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="showFingerprintInfo(scope.$index, scope.row)"
          >
            地图显示
          </el-button>
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="devices.length"
    ></el-pagination>
    <el-dialog
      :title="editDialogTitle"
      :visible.sync="editDialogVisible"
      width="30%"
      :show-close="true"
      :destroy-on-close="true"
      @close="handleCloseDialog"
    ></el-dialog>
  </el-main>
</template>

<script>
import dayjs from 'dayjs'
// import axios from 'axios'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'DeviceTable',
  data() {
    return {
      editDialogTitle: '指纹信息',
      editDialogVisible: false,
      ableInsert: false,
      editForm: {
        mac: '',
        name: '',
        info: '',
      },
      currentPage: 1,
      pageSize: 10,
    }
  },

  methods: {
    ...mapActions(['getDevices']),
    // 分页操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
    },
    handleCloseDialog() {
      console.log('closeDialog')
    },
    showFingerprintInfo(index, row) {
      console.log('showFingerprintInfo', index, row)
    },
    handleEdit(row) {
      console.log('handleEdit', row)
    },
    handleDelete(index, row) {
      console.log('handleDelete', index, row)
    },
    handleFlash() {
      console.log('handleFlash')
    },
    handleAdd() {
      console.log('handleAdd')
    },
  },
  computed: {
    ...mapState(['devices']),
    sliceDevices() {
      return this.devices.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    },
  },
  filters: {
    // 时间格式化
    timeFormate(value, str = 'YYYY/MM/DD HH:mm:ss') {
      // console.log('@',value)
      if (value != null) {
        return dayjs(value).format(str)
      } else {
        return '无最近记录'
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
